package com.hangzhou.welbeing.welbeinginstrument.fragement;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.hangzhou.welbeing.welbeinginstrument.R;
import com.hangzhou.welbeing.welbeinginstrument.activity.FansActivity;
import com.hangzhou.welbeing.welbeinginstrument.activity.InviteYourFamilyActivity;
import com.hangzhou.welbeing.welbeinginstrument.activity.MyCollectionActivity;
import com.hangzhou.welbeing.welbeinginstrument.activity.MyDeviceActivity;
import com.hangzhou.welbeing.welbeinginstrument.activity.MyStateActivity;
import com.hangzhou.welbeing.welbeinginstrument.activity.PayAttentionActivity;
import com.hangzhou.welbeing.welbeinginstrument.activity.PersonalInformationActivity;
import com.hangzhou.welbeing.welbeinginstrument.activity.SettingActivity;
import com.hangzhou.welbeing.welbeinginstrument.activity.SimilarProfilePageFocusedActivity;
import com.hangzhou.welbeing.welbeinginstrument.adapter.MeFragmentAdapter;
import com.hangzhou.welbeing.welbeinginstrument.base.MyApplication;
import com.hangzhou.welbeing.welbeinginstrument.bean.BasicDateBean;
import com.hangzhou.welbeing.welbeinginstrument.bean.MyPostDateBean;
import com.hangzhou.welbeing.welbeinginstrument.config.RBConstants;
import com.hangzhou.welbeing.welbeinginstrument.inteface.OnRecyclerViewItemClickListener;
import com.hangzhou.welbeing.welbeinginstrument.tool.GlideCircleTransform;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

/**
 * 时间 2017 年 08 月 21.
 * 作者 Administrator  刘传华
 * 公司 杭州惟君医疗科技有限公司
 * 包名 com.hangzhou.welbeing.pregnant.welbeing_pregnant.fragement
 * 工程名 welbeing_pregnant
 */
public class MeFragment extends Fragment {
    private static final String TAG ="MeFragment" ;
    @Bind(R.id.fragment_me_iv_setting)
    ImageView fragmentMeIvSetting;
    @Bind(R.id.fragment_me_iv_head_portrait)
    ImageView fragmentMeIvHeadPortrait;
    @Bind(R.id.fragment_me_tv_name)
    TextView fragmentMeTvName;
    @Bind(R.id.fragment_me_tv_labelling)
    TextView fragmentMeTvLabelling;
    @Bind(R.id.fragment_me_tv_state)
    TextView fragmentMeTvState;
    @Bind(R.id.fragment_me_iv_edit_data)
    ImageView fragmentMeIvEditData;
    @Bind(R.id.fragment_me_tv_edit_data)
    TextView fragmentMeTvEditData;
    @Bind(R.id.fragment_me_ll_edit_data)
    LinearLayout fragmentMeLlEditData;
    @Bind(R.id.fragment_me_rl_personal_information)
    RelativeLayout fragmentMeRlPersonalInformation;
    @Bind(R.id.fragment_me_tv_focus_on)
    TextView fragmentMeTvFocusOn;
    @Bind(R.id.fragment_me_tv_focus_on_number)
    TextView fragmentMeTvFocusOnNumber;
    @Bind(R.id.fragment_me_rl_focus_on)
    RelativeLayout fragmentMeRlFocusOn;
    @Bind(R.id.fragment_me_tv_fans)
    TextView fragmentMeTvFans;
    @Bind(R.id.fragment_me_tv_fans_number)
    TextView fragmentMeTvFansNumber;
    @Bind(R.id.fragment_me_rl_fans)
    RelativeLayout fragmentMeRlFans;
    @Bind(R.id.fragment_me_tv_collect)
    TextView fragmentMeTvCollect;
    @Bind(R.id.fragment_me_tv_collect_number)
    TextView fragmentMeTvCollectNumber;
    @Bind(R.id.fragment_me_rl_collect)
    RelativeLayout fragmentMeRlCollect;
    @Bind(R.id.fragment_me_iv_my_state)
    ImageView fragmentMeIvMyState;
    @Bind(R.id.fragment_me_rl_my_state)
    RelativeLayout fragmentMeRlMyState;
    @Bind(R.id.fragment_me_iv_invite_your_family)
    ImageView fragmentMeIvInviteYourFamily;
    @Bind(R.id.fragment_me_rl_invite_your_family)
    RelativeLayout fragmentMeRlInviteYourFamily;
    @Bind(R.id.fragment_me_iv_my_device)
    ImageView fragmentMeIvMyDevice;
    @Bind(R.id.fragment_me_rl_my_device)
    RelativeLayout fragmentMeRlMyDevice;
    @Bind(R.id.fragment_me_recyclerView)
    RecyclerView fragmentMeRecyclerView;
    private Context mcontext = MyApplication.getMcontext();
    private MeFragmentAdapter meFragmentAdapter;
    private List<MyPostDateBean.DataBean> mlist;
    private BasicDateBean.DataBean bean;
    private int lastVisibleItem_01;
    private int p2= 1;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = View.inflate(mcontext, R.layout.fragment_me, null);
        ButterKnife.bind(this, view);

        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        fragmentMeRecyclerView.setLayoutManager(layoutManager);
        loadBasicDate();
        return view;
    }
    /***
     * @desc 我发布的帖子的接口
     *
     */
    private void loadMyPostDate(int p) {
        SharedPreferences sp =mcontext.getSharedPreferences("second_app_internal_data", 0);
        String user_id = sp.getString("user_id","");
        OkHttpUtils
                .post()
                .url(RBConstants.SERVER_URL+RBConstants.USER_POST_TABULATION)
                .addParams("cus_id", user_id)
                .addParams("p", p+"")
                .build()
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String praiseParseNetworkResponse =  response.body().string();
                        Gson gson = new Gson();
                        Log.e(TAG,"praiseParseNetworkResponse============>"+praiseParseNetworkResponse);
                        final MyPostDateBean myPostDateBean  =  gson.fromJson(praiseParseNetworkResponse,MyPostDateBean.class);
                        if(myPostDateBean.errorCode == 200 ){
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                    mlist = myPostDateBean.data;
                                    meFragmentAdapter = new MeFragmentAdapter(mlist,mcontext);
                                    fragmentMeRecyclerView.setAdapter(meFragmentAdapter);
                                    meFragmentAdapter.setOnItemClickListener(new OnRecyclerViewItemClickListener() {
                                        @Override
                                        public void onItemClick(View view) {
                                            int position = fragmentMeRecyclerView.getChildAdapterPosition(view);
                                            Intent intent = new Intent(getContext(), SimilarProfilePageFocusedActivity.class);
                                            intent.putExtra("po_id", mlist.get(position).po_id);
                                          // intent.putExtra("po_url", mlist.get(position).po_url);
                                            intent.putExtra("po_name", mlist.get(position).po_name);
                                            intent.putExtra("content", mlist.get(position).content);
                                            intent.putExtra("cus_logo", bean.cus_logo);
                                            intent.putExtra("addtime", mlist.get(position).addtime);
                                            intent.putExtra("cus_name", bean.cus_name);
                                            //intent.putExtra("state", mlist.get(position).state+"");
                                            intent.putExtra("count", mlist.get(position).count+"");
                                            intent.putExtra("cus_id", mlist.get(position).cus_id+"");
                                            intent.putExtra("col", mlist.get(position).col+"");
                                            intent.putExtra("lCount", mlist.get(position).lCount+"");
                                            intent.putExtra("lstate", mlist.get(position).lstate+"");
                                            intent.putExtra("hits_like", mlist.get(position).hits_like+"");
                                            startActivity(intent);
                                        }

                                        @Override
                                        public void onItemFirstButtonClick(View v, int position) {

                                        }
                                    });
                                }
                            });
                        }else{
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(MyApplication.getMcontext(),myPostDateBean.errorMessage,Toast.LENGTH_SHORT).show();
                                }
                            });
                        }
                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });
    }

    /**
     * @desc 加载基本资料
     * */
    private void loadBasicDate() {
        SharedPreferences sp =mcontext.getSharedPreferences("second_app_internal_data", 0);
        String user_id = sp.getString("user_id","");
        OkHttpUtils
                .post()
                .url(RBConstants.SERVER_URL+RBConstants.PERSONAL_CENTER)
                .addParams("cus_id",user_id)
                .build()
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String praiseParseNetworkResponse =  response.body().string();
                        Gson gson = new Gson();
                        Log.e(TAG,"praiseParseNetworkResponse============>"+praiseParseNetworkResponse);
                        final BasicDateBean basicDateBean =   gson.fromJson(praiseParseNetworkResponse,BasicDateBean.class);
                        if(basicDateBean.errorCode == 200){
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                    bean = basicDateBean.data;

                                   Glide.with(mcontext).load(RBConstants.SERVER_URL+"/Public/Uploads/"+basicDateBean.data.cus_logo).transform(new GlideCircleTransform(mcontext)).into(fragmentMeIvHeadPortrait);
                                    fragmentMeTvName.setText(basicDateBean.data.cus_name);
                                    SharedPreferences sp =mcontext.getSharedPreferences("second_app_internal_data", 0);
                                    SharedPreferences.Editor edit = sp.edit();
                                    edit.putString("tel",basicDateBean.data.tel);
                                    edit.putString("birthday",basicDateBean.data.birthday);
                                    edit.putString("state",basicDateBean.data.state);
                                    edit.putString("cus_logo",basicDateBean.data.cus_logo);
                                    edit.putString("qq",basicDateBean.data.qq);
                                    edit.putString("weixin",basicDateBean.data.weixin);
                                    edit.putString("cus_logo",basicDateBean.data.cus_logo);
                                    edit.putString("cus_name",basicDateBean.data.cus_name);
                                    edit.commit();

                                    fragmentMeTvFocusOnNumber.setText(basicDateBean.data.follower);
                                    fragmentMeTvFansNumber.setText(basicDateBean.data.followed);
                                    fragmentMeTvCollectNumber.setText(basicDateBean.data.col+"");
                                   String  signature =  basicDateBean.data.signature;
                                    if(signature.equals("")){

                                    }else{
                                        fragmentMeTvLabelling.setText(signature);
                                    }


                                    loadMyPostDate(p2);

                                }
                            });

                        }else{
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(MyApplication.getMcontext(),basicDateBean.errorMessage,Toast.LENGTH_SHORT).show();
                                }
                            });
                        }

                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });
    }


    @OnClick({R.id.fragment_me_iv_setting, R.id.fragment_me_ll_edit_data, R.id.fragment_me_rl_focus_on, R.id.fragment_me_rl_fans, R.id.fragment_me_rl_collect, R.id.fragment_me_rl_my_state, R.id.fragment_me_rl_invite_your_family, R.id.fragment_me_rl_my_device})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.fragment_me_iv_setting:
                Intent intent = new Intent(getContext(), SettingActivity.class);
                startActivity(intent);
                break;
            case R.id.fragment_me_ll_edit_data:  //个人信息
                Intent intent1 = new Intent(getContext(), PersonalInformationActivity.class);
                startActivity(intent1);
                break;
            case R.id.fragment_me_rl_focus_on:  //关注 PayAttentionActivity
                SharedPreferences sp = mcontext.getSharedPreferences("second_app_internal_data", 0);
                String user_id = sp.getString("user_id","");
                Intent intent2 = new Intent(getContext(), PayAttentionActivity.class);
                intent2.putExtra("id",user_id);
                startActivity(intent2);
                break;
            case R.id.fragment_me_rl_fans:  //粉丝  FansActivity
                SharedPreferences sp01 = mcontext.getSharedPreferences("second_app_internal_data", 0);
                String user_id_01 = sp01.getString("user_id","");
                Intent intent3 = new Intent(getContext(), FansActivity.class);
                intent3.putExtra("id",user_id_01);
                startActivity(intent3);
                break;
            case R.id.fragment_me_rl_collect: //收藏 MyCollection
                Intent intent4 = new Intent(getContext(), MyCollectionActivity.class);
                startActivity(intent4);
                break;
            case R.id.fragment_me_rl_my_state:
                Intent intent5 = new Intent(getContext(), MyStateActivity.class);
                startActivity(intent5);
                break;
            case R.id.fragment_me_rl_invite_your_family: //邀请家人 Invite your family
                Intent intent6 = new Intent(getContext(), InviteYourFamilyActivity.class);
                startActivity(intent6);
                break;
            case R.id.fragment_me_rl_my_device:
                Intent intent7 = new Intent(getContext(), MyDeviceActivity.class);
                startActivity(intent7);
                break;
        }
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
